package com.erp.module.zhiLiang.xingJianZhiLiang

import android.content.Context
import android.util.Log
import com.erp.bean.ZhiLiangBean
import com.erp.mclass.UnCeHandler
import com.erp.Interface.OnUserDataInterface
import com.erp.bean.PhotoBean
import com.erp.util.JsonUtil
import com.erp.util.Myutil
import org.json.JSONException
import org.json.JSONObject
import java.util.*
/**
 * Created by Administrator on 2018/3/27/027.
 * 作者：刘洋
 */
class IXJWuLiaoZhiLiangKp(val xjZhiLiangKView: XjZhiLiangKView, val context: Context, var zhiliangbean: ZhiLiangBean, var code: Int) : XJZhiLiangKP {

    var md = XJWuLiaoZhiLiangModel()
    companion object {
        var zhiliangs = ""
        var gonghaos = ""
        var params = ""
    }

    var rightcode = 0
    var chongfu=false
    override fun MoShi() {
        UnCeHandler.buzhou="MoShi"
        if (rightcode == 0) {
            if (code == 1) {
                rightcode = 2
                xjZhiLiangKView.XiuGai()
            } else if (code == 2) {
                rightcode = 1
                xjZhiLiangKView.ChaKan()
            }
        } else if (rightcode == 1) {
            rightcode = 2
            xjZhiLiangKView.XiuGai()
        }
    }

    override fun setZhiLiangBean(zhiliangbean: ZhiLiangBean) {
        this.zhiliangbean = zhiliangbean
    }

    override fun getWuLiaoZhiLiangDaiMa() {
        UnCeHandler.buzhou="getWuLiaoZhiLiangDaiMa"
        if(zhiliangs !=""){
            getzhialiangjson(zhiliangs)
            return
        }
        md.getZhiLiangDaiMa(context, object : OnUserDataInterface {
            override fun onUserData(data: String) {
                Myutil.MyTost("获取问题列表")
                zhiliangs = data
                getzhialiangjson(data)
            }

            override fun onNot(throwable: String) =
                    xjZhiLiangKView.getWuLiaoZhiLiangDaiMaNo(throwable)
        })
    }
    fun getzhialiangjson(data: String){
        UnCeHandler.buzhou="getzhialiangjson"
        var zhiliangid: MutableList<String> = ArrayList()
        var zhiliangname: MutableList<String> = ArrayList()
        try {
            val jo = JSONObject(data)
            val keys = jo.keys()
            while (keys.hasNext()) {
                val key = keys.next() as String
                zhiliangid.add(key)
                zhiliangname.add(JsonUtil.getvolue(jo, key))
            }
            xjZhiLiangKView.getWuLiaoZhiLiangDaiMaOk(zhiliangid, zhiliangname)
        } catch (e: Exception) {
            Log.d("TAAAG", "e=" + e.toString())
            e.printStackTrace()
        }
    }
    override fun putWuLiaoZHiLiang(zhiliangbeann: ZhiLiangBean, deletephoto: MutableList<PhotoBean>) {
        UnCeHandler.buzhou="putWuLiaoZHiLiang"
        var shuangchuancode=0
        if(chongfu){
            shuangchuancode=3
        }else{
            shuangchuancode=code
        }
        md.setZhiLiang(context, shuangchuancode, zhiliangbeann, deletephoto, object : OnUserDataInterface {
            override fun onUserData(data: String) {
                Log.d("TAAAG", "data=" + Myutil.decodeUnicode(data))
                Myutil.MyTost("上传成功")
                if (code == 2) {
                    try {
                        val jo = JSONObject(data)
                        zhiliangbean.id = JsonUtil.getvolue(jo, "id")
                        zhiliangbean.mcode = JsonUtil.getvolue(jo, "mcode")
                        zhiliangbean.work_number = JsonUtil.getvolue(jo, "work_number")
                        zhiliangbean.occurrence_time = JsonUtil.getvolue(jo, "occurrence_time")
                        zhiliangbean.description = JsonUtil.getvolue(jo, "description")
                        zhiliangbean.handle_way = JsonUtil.getvolue(jo, "handle_way")
                        zhiliangbean.ctime = JsonUtil.getvolue(jo, "ctime")
                        zhiliangbean.qi_name = JsonUtil.getvolue(jo, "qi_name")
                        zhiliangbean.qi_id = JsonUtil.getvolue(jo, "qi_id")
                        zhiliangbean.creator_name = JsonUtil.getvolue(jo, "creator_name")
                        zhiliangbean.dnum = JsonUtil.getvolue(jo, "dnum")
                        zhiliangbean.solution = JsonUtil.getvolue(jo, "solution")
                        val jsp = JsonUtil.getjsvolue(jo, "files")
                        zhiliangbean.listphoto.clear()
                        if (jsp != null) {
                            for (j in 0 until jsp.length()) {
                                val jop = jsp.get(j) as JSONObject
                                val photo = PhotoBean()
                                photo.fid = JsonUtil.getvolue(jop, "fid")
                                photo.filename = JsonUtil.getvolue(jop, "filename")
                                photo.uri = JsonUtil.getvolue(jop, "uri")
                                zhiliangbean.listphoto.add(photo)
                            }
                        }
                    } catch (e: JSONException) {
                        e.printStackTrace()
                    }

                    xjZhiLiangKView.putWuLiaoZhiLiangshujuOk()
                } else if (code == 1) {
                    xjZhiLiangKView.addWuLiaoZhiLiangshujuOk()
                }
            }

            override fun onNot(throwable: String) {
                Log.d("TAAG","th="+throwable)
                xjZhiLiangKView.putWuLiaoZhiLiangshujuNo(throwable)
            }
        })
    }
    override fun getGongHaoLieBiao() {
        UnCeHandler.buzhou="getGongHaoLieBiao"
       if(gonghaos !=""){
           getgonghaojson(gonghaos)
           return
       }
        md.getZhiLiangGongHaoLieBiao(context, object : OnUserDataInterface {
            override fun onUserData(data: String) {
                Myutil.MyTost("获取工号列表")
                gonghaos = data
                getgonghaojson(data)
            }

            override fun onNot(throwable: String?) {
                Log.d("TAAAG", "throwable=" + Myutil.decodeUnicode(throwable))
                if (throwable != null) {
                    xjZhiLiangKView.getGongHaoLieBiaoNo(throwable)
                }
            }

        })
    }
    fun getgonghaojson(data: String){
        UnCeHandler.buzhou="getgonghaojson"
        var gonghao: MutableList<String> = ArrayList()
        var gonghaoname: MutableList<String> = ArrayList()
        try {
            val jo = JSONObject(data)
            val keys = jo.keys()
            while (keys.hasNext()) {
                val key = keys.next() as String
                gonghao.add(key)
                gonghaoname.add(JsonUtil.getvolue(jo, key))
            }
            xjZhiLiangKView.getGongHaoLieBiaoOk(gonghao, gonghaoname)
        } catch (e: Exception) {
            Log.d("TAAAG", "e=" + e.toString())
            e.printStackTrace()
        }
    }
    override fun chongFuShaungChuan(chongfu: Boolean) {
        this.chongfu=chongfu
    }

    override fun getmaterial_quality_params() {
        if(params !=""){
            getparamsjson(params)
            return
        }
        md.getmaterial_quality_params(context, object : OnUserDataInterface {
            override fun onUserData(data: String) {
                params = data
                getparamsjson(data)
            }

            override fun onNot(throwable: String?) {

            }

        })
    }

    fun getparamsjson(data: String){
        try {
            Log.d("getparamsjson",data)
            var jitainame: MutableList<String> = ArrayList()
            var miaoshuname: MutableList<String> = ArrayList()
            var fanganname: MutableList<String> = ArrayList()
            val j = JSONObject(data)
            val jo = JsonUtil.getjovolue(j,"data")
            val jitaijs = JsonUtil.getjsvolue(jo,"machine_number")
            val miaoshujs = JsonUtil.getjsvolue(jo,"quality_reason")
            val fanganjs = JsonUtil.getjsvolue(jo,"solve_scheme")
            Log.d("getparamsjson",jitaijs.toString())
            for (j in 0 until jitaijs.length()){
                jitainame.add(jitaijs[j].toString())
            }
            for (j in 0 until miaoshujs.length()){
                miaoshuname.add(miaoshujs[j].toString())
            }
            for (j in 0 until fanganjs.length()){
                fanganname.add(fanganjs[j].toString())
            }
            xjZhiLiangKView.getmaterial_quality_paramsOk(jitainame,miaoshuname,fanganname)
        } catch (e: JSONException) {
            e.printStackTrace()
        }
    }

    override fun querenShouhuo() {
        md.querenShouhuo(context,zhiliangbean.id,object :OnUserDataInterface{
            override fun onUserData(data: String?) {
                xjZhiLiangKView.querenShouhuoOk()
            }

            override fun onNot(throwable: String?) {
                xjZhiLiangKView.querenShouhuoNo()
            }

        })
    }

}